package org.genntii.warehousesystem.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.genntii.warehousesystem.domain.entity.Goods;
import org.genntii.warehousesystem.domain.entity.GoodsType;
import org.genntii.warehousesystem.domain.entity.Type;
import org.genntii.warehousesystem.mapper.GoodsMapper;
import org.genntii.warehousesystem.mapper.GoodsTypeMapper;
import org.genntii.warehousesystem.mapper.TypeMapper;
import org.genntii.warehousesystem.service.GoodsTypeService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsTypeServiceImpl extends ServiceImpl<GoodsTypeMapper, GoodsType> implements GoodsTypeService {

    @Resource
    private GoodsTypeMapper goodsTypeMapper;

    @Resource
    private TypeMapper typeMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public void typeAdds(List<Integer> typeIds, int goodsId) {
        for (int id:typeIds){
            GoodsType goodsType = new GoodsType();
            goodsType.setTypeId(id);
            goodsType.setGoodsId(goodsId);
            goodsTypeMapper.insert(goodsType);
        }

    }

    @Override
    public List<Type> showAllTypeByGoodsId(int goodsId) {
        List<GoodsType> goodsTypeList = goodsTypeMapper.showAllGoodsTypeByGoodsId(goodsId);
        List<Type> types = new ArrayList<>();
        for (GoodsType goodsType:goodsTypeList){
            Type type = typeMapper.selectById(goodsType.getTypeId());
            types.add(type);
        }
        return types;
    }

    @Override
    public void del(int typeId, int goodsId) {
        goodsTypeMapper.del(goodsId,typeId);
    }

    @Override
    public List<Goods> selectGoodsByTypeId(int typeId, int page) {
        List<Integer> goodsIds = goodsTypeMapper.selectGoodsIdByTYpeId(typeId,10*(page-1));
        List<Goods> goodsList = new ArrayList<>();
        for (int id:goodsIds){
            Goods goods = goodsMapper.selectById(id);
            goodsList.add(goods);
        }
        return goodsList;
    }
}
